package service;

import java.math.BigDecimal;
import java.util.List;

import beans.Agent;
import beans.Courier;

import dao.CompanyDao;
import dao.CourierDao;
import dao.QueryDAO;

import queryterm.CourierQuery;
import utils.PageBean;

public class CourierService implements CourierServiceInterface{
    
    private List<Courier> couriers = null;
    
	CourierDao courierDao = new CourierDao();
	QueryDAO queryDao = new QueryDAO();
	CompanyDao companyDao = new CompanyDao();
	
	String sql = null;
    String hql = null;
	
	/* (non-Javadoc)
	 * @see Service.CourierServiceinterface#getPackageOfpage(java.lang.Integer, java.lang.Integer, queryterm.CourierQuery, Beans.Courier)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public PageBean<Courier> getCourierOfPage(Integer curpage, Integer pageSize,
			CourierQuery courierQuery,Agent agent) {
		StringBuffer buff = new StringBuffer();
		if (courierQuery != null) {
			/*if (courierQuery.getDimparam() != null
					&& !"".equals(courierQuery.getDimparam())) {
				buff.append(" and ( c.rfidCardRfid like '%"
						+ courierQuery.getDimparam() + "%'");
				buff.append(" or c.rfidCardSernum like '%"
						+ courierQuery.getDimparam() + "%')");
			}*/
			if (courierQuery != null) {
				if (courierQuery.getCompanyID() != -1) {
					buff.append(" and  c.company like '%"
							+ companyDao.getCompanyById(courierQuery.getCompanyID()).getCompanyName() + "%'");
				}
				if (courierQuery.getExpressID() != -1) {
					buff.append(" and  c.express.expressId = "
							+ courierQuery.getExpressID());
				}
				if (courierQuery.getName() != null
						&& !courierQuery.getName().equals("")) {
					buff.append(" and  c.courierName like '%"
							+ courierQuery.getName() + "%'");
				}
				if (courierQuery.getPhoneNum() != null
						&& !courierQuery.getPhoneNum().equals("")) {
					buff.append(" and  c.mobileNum like '%"
							+ courierQuery.getPhoneNum() + "%'");
				}
				if (courierQuery.getRfid() != null
						&& !courierQuery.getRfid().equals("")) {
					buff.append(" and  c.rfid like '%"
							+ courierQuery.getRfid() + "%'");
				}
			}
		}
		String hql = null;
		String hqlcount = null;
		hql = "from Courier c where c.express.agent.id = "+agent.getId()
				+ buff.toString() + " order by c.courierId";
		hqlcount = "select count(c.courierId) from Courier c where c.express.agent.id = "+agent.getId()
				+ buff.toString() + " order by c.courierId";
		return new PageBean<Courier>(curpage, pageSize,
				Integer.parseInt(queryDao.queryUnique(hqlcount) + ""),
				queryDao.queryByPage(hql, curpage, pageSize));
	}
	@Override
	public BigDecimal getAllPayIn(Courier courier){
		String hql = "select sum(l.money) from LogPay l where l.status = 'success' and l.express.expressId = "+courier.getExpress().getExpressId();
		Object object = queryDao.queryUnique(hql);
		if(object==null){
			return new BigDecimal(0);
		}else{
//			return (BigDecimal)object;
			return ((BigDecimal)object).setScale(2,BigDecimal.ROUND_HALF_UP);
//			return new BigDecimal((Double)object).setScale(2,BigDecimal.ROUND_HALF_UP);
//			return (double) Math.round((Double)object);
		}
	}
	@Override
	public BigDecimal getAllPayOut(Courier courier){
		String hql = "select sum(l.money) from LogPayPackage l where l.status = 0 and l.express.expressId = "+courier.getExpress().getExpressId();
		Object object = queryDao.queryUnique(hql);
		if(object==null){
			return new BigDecimal(0);
		}else{
//			return (BigDecimal)object;
//			return (double) Math.round((Double)object);
//			return new BigDecimal((Double)object).setScale(2,BigDecimal.ROUND_HALF_UP);
			return ((BigDecimal)object).setScale(2,BigDecimal.ROUND_HALF_UP);
		}
	}
	
	public List<Courier> getCouriers(Agent agent, int rows, int page) {
	    hql = "from Courier c where c.express.agent.id = ? order by c.courierId";
        return (List<Courier>)queryDao.queryByPage(hql, page,rows,agent.getId());
    }

    public int getCourierNumber(Agent agent) {
        hql = "select count(c.courierId) from Courier c where c.express.agent.id = ?";
        return ((Long)queryDao.queryUnique(hql, agent.getId())).intValue();
    }
    
    public void deleteCourier(String ids) {//delete 操作不能用表别名
        sql = "update courier set courier_status=?,courier_RFID=?,courier_sernum=? where courier_id in ("+ids+")";
        queryDao.queryExcute(sql,1,null,null);
        sql = "update express set express_courier_id=? where express_courier_id in ("+ids+")";
        queryDao.queryExcute(sql,null);
    }
	
}
